package com.itheima.thread.Demo6;

public class Ticket implements Runnable {
    private int ticket = 100;
    @Override
    public void run() {
        while(true){

            boolean res = method();
            if(res){
                break;
            }
        }
    }
    public synchronized boolean method(){
        if(ticket == 0){
            return true;
        }else{
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            ticket--;
            System.out.println(Thread.currentThread().getName()+"正在买票,还剩"+ticket+"张票");
            return false;
        }
    }
}
